package com.huayue.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.Menu;
import com.huayue.entity.MenuIndex;
import com.huayue.entity.User;
import com.huayue.service.MenuService;
import com.huayue.util.JSONHelper;
import com.huayue.util.MapToBean;
import com.huayue.util.PageHelper;

/**
 * 菜单
 *
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService svc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("status", 200);
        view.addStaticAttribute("data", svc.findAllTopMenu());
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-菜单")
    @RequestMapping("/add")
    public ModelAndView add(Menu menu) {
        return bc.add(svc, menu);
    }

    @LogAnno(operateType = "修改-菜单")
    @RequestMapping("/update")
    public ModelAndView update(Menu menu) {
        return bc.update(svc, menu);
    }

    @LogAnno(operateType = "删除-菜单")
    @RequestMapping("/delete")
    public ModelAndView delete(Menu menu) {
        return bc.delete(svc, menu);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(Menu menu) {
        return bc.findOne(svc, menu);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, Menu menu) {
        return bc.findPage(svc, page, menu, menu.getParentid());
    }

    @RequestMapping("/findMenuByUser")
    public ModelAndView findMenuByUser(HttpServletRequest request) throws Exception {
        FastJsonJsonView view = new FastJsonJsonView();
        User loginUser = (User)request.getSession().getAttribute("USER");

        JSONArray jsonArray =
            new JSONArray(MapToBean.convertListMap2ListBean(svc.findMenuByUser(loginUser), MenuIndex.class));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", 0);
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("data", JSONHelper.getJsontreeTwo(jsonArray, jsonObject).getJSONArray("list"));
        return new ModelAndView(view);
    }

}
